package vn.edu.saigontech.ncms.web.offering;

import java.util.Date;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.teaching.VideoSchedule;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.VideoScheduleService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class VideoScheduleAction extends BaseAction {
	private Long id;
	private Long offeringId;
	private String date;
	private String videoName;
	private String comment;

	public String showAdd() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}

	public String list() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		if (offering.getVideoSchedules().isEmpty())
			return ADD;
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}
	
	public String add() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Date dateDate = DateUtil.fromString(this.date, this.i18n("format.date"));
		
		VideoSchedule videoSchedule = new VideoSchedule();
		videoSchedule.setOffering(offering);
		videoSchedule.setDate(dateDate);
		videoSchedule.setVideo(this.videoName);
		videoSchedule.setComment(this.comment);
		
		VideoScheduleService.INSTANCE.persist(videoSchedule);
		this.offeringId = offering.getId();
		
		this.addLoggingDetail(BaseAction.ADD, "logging.video.add", videoSchedule.getVideo(), offering.getName());
		return SUCCESS;
	}
	
	public String showEdit() {
		VideoSchedule videoSchedule = VideoScheduleService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("schedule", videoSchedule);
		return SUCCESS;
	}
	
	public String edit() {
		VideoSchedule videoSchedule = VideoScheduleService.INSTANCE.findById(this.id);
		Date dateDate = DateUtil.fromString(this.date, this.i18n("format.date"));
		videoSchedule.setDate(dateDate);
		videoSchedule.setVideo(this.videoName);
		videoSchedule.setComment(this.comment);
		
		Offering offering = videoSchedule.getOffering(); 
		this.offeringId = offering.getId();
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.video.edit", videoSchedule.getVideo(), offering.getName());
		return SUCCESS;
	}
	
	public String delete() {
		VideoSchedule video = VideoScheduleService.INSTANCE.findById(this.id);
		String videoName = video.getVideo();
		String offeringName = video.getOffering().getName();
		
		VideoScheduleService.INSTANCE.deleteById(this.id);
		
		this.addLoggingDetail(BaseAction.DELETE, "logging.video.delete", videoName, offeringName);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getVideoName() {
		return videoName;
	}

	public void setVideoName(String videoName) {
		this.videoName = videoName;
	}
}
